package com.bysc.commentservice.controller;


import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commentservice.entity.CommentSon;
import com.bysc.commentservice.entity.vo.CommentFormVo;
import com.bysc.commentservice.entity.vo.CommentQueryVo;
import com.bysc.commentservice.entity.vo.SonCommentFormVo;
import com.bysc.commentservice.service.CommentSonService;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.commonutils.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-06-15
 */
@RestController
@Api(tags = "子评论管理")
@RequestMapping("/commentservice/comment-son")
public class CommentSonController {
    @Autowired
    private CommentSonService commentSonService;

    @ApiOperation(value = "根据PpCommentId和parentCommentId和postId和Token新增子评论")
    @PostMapping("addSonCommentByCommentFormVoAndToken")
    public Results addSonCommentByCommentFormVoAndToken(@ApiParam(name = "commentFormVo", value = "子评论信息", required = true)
                                                        @RequestBody SonCommentFormVo sonCommentFormVo, HttpServletRequest httpRequest) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(httpRequest);
            CommentSon commentSon = commentSonService.addSonCommentByCommentFormVoAndToken(sonCommentFormVo, userId);
            return Results.ok().data("id", commentSon.getId()).message(MessageConstant.ADD_COMMENT_SUCCESS);
        }catch (BaiYunException e) {
            throw e;
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.ADD_COMMENT_FAIL);
        }
    }

    @ApiOperation(value = "根据PpCommentId和Token修改子评论")
    @PutMapping("updateSonCommentById/{id}")
    public Results updateSonCommentById(@ApiParam(name = "sonCommentVo", value = "评论信息", required = true)
                                        @RequestBody CommentFormVo sonCommentFormVo,
                                        @PathVariable Object id) {
        try {
            commentSonService.updateSonCommentById(sonCommentFormVo, id);
            return Results.ok().message(MessageConstant.EDIT_COMMENT_SUCCESS);
        } catch (BaiYunException e) {
            throw e;
        }catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.EDIT_COMMENT_FAIL);
        }

    }

    @ApiOperation(value = "根据SonId删除子评论")
    @DeleteMapping("deleteSonCommentById/{id}")
    public Results deleteSonCommentById(@PathVariable Object id) {
        try {
            CommentSon commentSon = commentSonService.deleteSonCommentById(id);
            return Results.ok().data("id", commentSon.getId()).message(MessageConstant.DELETE_COMMENT_SUCCESS);
        } catch (BaiYunException e) {
            throw e;
        }catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.DELETE_COMMENT_FAIL);
        }
    }

    @ApiOperation(value = "根据Token和PpCommentId分页查询最新子评论")
    @GetMapping("getSonCommentListByTokenAndPpCommentIdOrderByCreatTime/{id}/{page}/{limit}")
    public Results getSonCommentListByTokenAndPpCommentIdOrderByCreatTime(@ApiParam(name = "id", value = "父评论id", required = true)
                                                                          @PathVariable Object id, @ApiParam(name = "page", value = "查询页数", required = true)
                                                                          @PathVariable Integer page, @ApiParam(name = "limit", value = "查询数量", required = true)
                                                                          @PathVariable Integer limit, HttpServletRequest request) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(request);
            PageResult pageResult = commentSonService.getSonCommentListByTokenAndPpCommentIdOrderByCreatTime(userId, id, page, limit);
            return Results.ok().data("page", pageResult).message(MessageConstant.QUERY_COMMENT_SUCCESS);
        }catch (BaiYunException e) {
            throw e;
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_COMMENT_FAIL);
        }
    }

    @ApiOperation(value = "根据Token和PpCommentId分页查询点赞最多子评论")
    @GetMapping("getSonCommentListByTokenAndPpCommentIdOrderBylike/{id}/{page}/{limit}")
    public Results getSonCommentListByTokenAndPpCommentIdOrderBylike(@ApiParam(name = "id", value = "父评论id", required = true)
                                                                     @PathVariable Object id, @ApiParam(name = "page", value = "查询页数", required = true)
                                                                     @PathVariable Integer page, @ApiParam(name = "limit", value = "查询数量", required = true)
                                                                     @PathVariable Integer limit, HttpServletRequest request) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(request);
            PageResult pageResult = commentSonService.getSonCommentListByTokenAndPpCommentIdOrderBylike(userId, id, page, limit);
            return Results.ok().data("page", pageResult).message(MessageConstant.QUERY_COMMENT_SUCCESS);
        }catch (BaiYunException e) {
            throw e;
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_COMMENT_FAIL);
        }
    }

    @ApiOperation(value = "(带条件)根据分页查询最新子评论--管理接口")
    @PostMapping("getAllSonCommentListByQueryVoOrderByCreatTime/{page}/{limit}")
    public Results getAllSonCommentListByQueryVoOrderByCreatTime(@ApiParam(name = "page", value = "查询页数", required = true)
                                                                 @PathVariable Integer page, @ApiParam(name = "limit", value = "查询数量", required = true)
                                                                 @PathVariable Integer limit, @RequestBody CommentQueryVo queryVo) {
        try {
            PageResult pageResult = commentSonService.getAllSonCommentListByQueryVoOrderByCreatTime(page, limit, queryVo);
            return Results.ok().data("page", pageResult).message(MessageConstant.QUERY_COMMENT_SUCCESS);
        } catch (BaiYunException e) {
            throw e;
        }catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_COMMENT_FAIL);
        }
    }
}

